package com.dr.web.filter;

import com.dr.common.pojo.WeChatFinalValue;
import com.dr.common.ution.StringUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * Created by tugou on 2017/5/6.
 */
public class WeChatFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpServletResponse res = (HttpServletResponse)servletResponse;
        if(req.getRequestURI().equals(req.getContextPath()+"/student/sign")){
            Map parMap = req.getParameterMap();
            if (parMap.containsKey("code")){
                filterChain.doFilter(servletRequest,servletResponse);
            }
            else
                res.sendRedirect(WeChatFinalValue.WECHAT_MAIN);
        }
        HttpSession session = req.getSession();
        String openId = (String) session.getAttribute("openId");
        if(StringUtil.isEmpty(openId)){
            res.sendRedirect(WeChatFinalValue.WECHAT_MAIN);
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
